package com.poker.controller;

import com.poker.consts.Consts;
import com.poker.entity.Product;
import com.poker.service.ProductService;
import com.poker.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ClassName ProductController
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 11:29
 * @Version 1.0
 **/
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController{

    @Resource
    private ProductService productService;

    @RequestMapping("/list")
    public String list(Model model, Product product,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "50") Integer size) {
        Page<Product> data = productService.findAllProduct(page, size);

        model.addAttribute("data", data);
        model.addAttribute("product", product);
        return managerPage("product/list");
    }

    @RequestMapping("/findQuery")
    public String findQuery(Model model , Product product,
                            @RequestParam(defaultValue = "0") Integer page,
                            @RequestParam(defaultValue = "50") Integer size) {
        Page<Product> data = productService.queryProduct(page, size, product);

        model.addAttribute("data", data);
        model.addAttribute("product", product);
        return managerPage("product/list");
    }

    @RequestMapping("/edit")
    public String edit(Model model, String id, String editType) {
        Product product = new Product();

        if (Consts.EditType.EDIT.equals(editType)) product = productService.getById(id);

        model.addAttribute("product", product);
        model.addAttribute("editType", editType);
        return managerPage("product/edit");
    }

    @ResponseBody
    @RequestMapping("/doEdit")
    public String doEdit(Product product) {
        if (Misc.isEmpty(product) || Misc.isEmpty(product.getId())) toJson(false, "编辑信息为空", null);

        productService.update(product);

        return toJson(true, "success", null);
    }

    @ResponseBody
    @RequestMapping("/add")
    public String add(Product product) {
        if (Misc.isEmpty(product)) return toJson(false, "添加信息为空", null);

        productService.saveProduct(product);

        return toJson(true, "success", null);
    }

    @RequestMapping("/delete")
    public String delete(String id, Integer page) {
        if (Misc.isEmpty(id)) return toJson(false, "id信息为空", null);

        if (Misc.isEmpty(page)) return toJson(false, "page信息为空", null);

        Product product = productService.getById(id);
        if (Misc.isEmpty(product)) return toJson(false, "目标信息不存在", null);

        productService.deleteProduct(product);

        return redirect("/news/list?page=" + page);
    }

}
